// 用于操作数据库
let query = require('../db/index')
let sql = require('./commnet_sql')

// 查询全部文章评论数据 TODO
exports.getall = function (callback) {
    query(sql.selectAll, function (err, results, fields) {
        if (err) {
            console.log('数据查询失败');
            throw err;
        }
        callback(results);
    });
}

// 返回指定文章评论记录 {}
exports.get = function (comment_id, callback) {
    var arr = [];
    arr.push(comment_id);
    query(sql.selectOne, arr, function (err, results, fields) {
        if (err) {
            console.log('数据查询失败');
            throw err;
        }
        callback(results);
    });
}

// 返回指定文章评论记录 {}
exports.getOrigin = function (comment_id, callback) {
    var arr = [];
    arr.push(comment_id);
    query(sql.selectOneOrigin, arr, function (err, results, fields) {
        if (err) {
            console.log('数据查询失败');
            throw err;
        }
        callback(results);
    });
}

// 增加一条记录
exports.add = function (rec, callback) {
    query(sql.insertOne, rec, function (err, results, fields) {
        if (err) {
            console.log('[增加记录错误] - ', err.message);
            throw err;
        }
        callback(results);
    });
}

// 修改一条记录TODO
exports.update = function (values, callback) {
    query(sql.updateOne, values, function (err, results, fields) {
        if (err) {
            console.log('[修改记录错误] - ', err.message);
            throw err;
        }
        callback(results);
    });
}
// 删除一条记录
exports.del = function (comment_id, callback) {
    var arr = [];
    arr.push(comment_id);
    query(sql.deleteOne, arr, function (err, results, fields) {
        if (err) {
            console.log('[删除记录错误] - ', err.message);
            throw err;
        }
        callback(results);
    });
}
